﻿//using System;
//using System.Collections.Generic;
//using System.Text;

//namespace DCRF.Common.Support
//{
//    public delegate void PropertyItemUpdatingDelegate(PropertyItem sender, ref object value, ref bool cancelOperation);
//    public delegate void PropertyItemUpdatedDelegate(PropertyItem sender, object value);

//    public delegate void PropertyItemReadingDelegate(PropertyItem sender, ref object value, ref bool useProvidedValueAndAbort);
//    public delegate void PropertyItemReadDelegate(PropertyItem sender, ref object value);

//    /// <summary>
//    /// Encapsulates a single Property of a Block or a BlockWeb.
//    /// </summary>
//    [Serializable]
//    public class PropertyItem
//    {
//        /// <summary>
//        /// Real value of the property
//        /// </summary>
//        private object value = null;

//        /// <summary>
//        /// The type of value. Used for validation when value is going to be set.
//        /// </summary>
//        private Type validType = null;

//        /// <summary>
//        /// If this is true, no write operation is possible on this property item.
//        /// </summary>
//        private bool isReadOnly = false;

//        public event PropertyItemUpdatingDelegate UpdatingProperty;
//        public event PropertyItemUpdatedDelegate PropertyUpdated;

//        public event PropertyItemReadingDelegate ReadingProperty;
//        public event PropertyItemReadDelegate PropertyRead;

//        /// <summary>
//        /// Build a new instance of PropertyItem
//        /// </summary>
//        /// <param name="valid">Valid type for this property item</param>
//        /// <param name="initial"></param>
//        /// <param name="readOnly"></param>
//        public PropertyItem(Type valid, object initial, bool readOnly)
//        {
//            validType = valid;
//            value = initial;
//            isReadOnly = readOnly;
//        }

//        public object Value
//        {
//            get
//            {
//                object result = value;
//                bool useProvidedValueAndAbort = false;

//                if (ReadingProperty != null)
//                {
//                    ReadingProperty(this, ref result, ref useProvidedValueAndAbort);

//                    if (useProvidedValueAndAbort)
//                    {
//                        return result;
//                    }
//                }

//                if (PropertyRead != null)
//                {
//                    //here external code can modify result
//                    PropertyRead(this, ref result);
//                }

//                return result;
//            }
//            set
//            {
//                if (isReadOnly) throw new Exception("Property is readonly");

//                if (validType != null && value != null)
//                {
//                    if (validType.IsInstanceOfType(value) == false)
//                    {
//                        throw new Exception("Invalid type for property. correct type is:" + validType.ToString());
//                    }
//                }

//                object newValue = value;

//                if (UpdatingProperty != null)
//                {
//                    bool cancelOperation = false;

//                    UpdatingProperty(this, ref newValue, ref cancelOperation);

//                    if (cancelOperation)
//                    {
//                        //external handler has stated us to cancel update action
//                        return;

//                        //if external handler wants to modify value, it can do this
//                        //by using ref newValue passed to it but it will not be saved to this.value if it sets 
//                        //cancelOperation to true
//                    }
//                }

//                this.value = value;

//                if (PropertyUpdated != null)
//                {
//                    PropertyUpdated(this, newValue);
//                }
//            }
//        }
//    }
//}
